package com.beehoo.dcportal.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.beehoo.dcportal.common.base.BaseDao;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.dao.ICommentDao;
import com.beehoo.dcportal.model.CommentBean;

public class CommentDaoImpl extends BaseDao implements ICommentDao {
	private static final Logger logger = Logger.getLogger(CommentDaoImpl.class);
	
	/**
	 * 获取信息评论列表
	 * @param streetId
	 * @param msgId
	 * @param commentState
	 * @param iPageNo
	 * @param iRows
	 * @return
	 * @throws BaseException
	 */
	@SuppressWarnings("unchecked")
	public List<CommentBean> getCommentList(String streetId, String msgId, 
			CommentStatus commentState, int iPageNo, int iRows) throws BaseException {
		List<CommentBean> msgList = new ArrayList<CommentBean>();
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("streetId", streetId);
		paramMap.put("msgId", msgId);
		paramMap.put("commentState", commentState.val());
		paramMap.put("msgRows", iRows);
		paramMap.put("offset", (iPageNo-1)*iRows);
		
		try {
			msgList = (List<CommentBean>) sqlMapClient.
							queryForList("portal-comment.queryCommentList", paramMap);
			logger.info("信息列表-获取信息列表|#调用portal-comment.queryCommentList " +
					"中心编号："+streetId+
					"信息类型："+msgId+
					"评论状态："+commentState.val()+
					"查询页码："+iPageNo+
					"查询条数："+iRows+",成功!");
		} catch (SQLException e) {
			logger.info("查询信息|#调用portal-comment.queryCommentList " +
					"中心编号："+streetId+
					"信息类型："+msgId+
					"评论状态："+commentState.val()+
					"查询页码："+iPageNo+
					"查询条数："+iRows+",失败!", e);
			throw new BaseException(e,"查询信息失败");
		}
		
		return msgList;
	}
	
	/**
	 * 获取信息评论总数
	 * @param streetId
	 * @param msgId
	 * @param commentState
	 * @return
	 * @throws BaseException
	 */
	public int getCommentCount(String streetId, String msgId, 
			CommentStatus commentState) throws BaseException {
		int msgCount = 0;
		Map<String, String> paramMap = new HashMap<String, String>();
		paramMap.put("streetId", streetId);
		paramMap.put("msgId", msgId);
		paramMap.put("commentState", commentState.val());
		
		try {
			msgCount = (Integer) sqlMapClient.queryForObject("portal-comment.getCommentCount", paramMap);
			logger.info("评论列表-获取评论总数|#调用portal-comment.getCommentCount " +
					"中心编号："+streetId+
					"评论状态："+commentState.val()+
					"信息编号："+msgId+ ",成功!");
		} catch (SQLException e) {
			logger.error("评论列表-获取评论总数|#调用portal-comment.getCommentCount " +
					"中心编号："+streetId+
					"评论状态："+commentState.val()+
					"信息编号："+msgId+",失败!", e);
			throw new BaseException(e,"查询评论总数失败");
		}
		
		return msgCount;
	}
	
	/**
	 * 添加信息评论
	 * @param comment
	 * @return
	 * @throws BaseException
	 */
	public int addComment(CommentBean comment) throws BaseException {
		try {
			Object o = sqlMapClient.insert("portal-comment.addComment", comment);
			System.out.println(o);
			logger.info("评论信息-添加评论|#调用portal-comment.addComment " +
					"中心编号："+comment.getStreetId()+
					"信息编号："+comment.getMsgId()+
					"评论内容："+comment.getCommentContent()+
					"评论人："+comment.getCommentAuthor()+
					"身份证："+comment.getAuthorIdcard()+
					"地址："+comment.getAuthorAddr()+
					"电子邮箱："+comment.getAuthorEmail()+
					"手机号码："+comment.getAuthorPhone()+",成功!");
		} catch (SQLException e) {
			logger.error("评论信息-添加评论|#调用portal-comment.addComment " +
					"中心编号："+comment.getStreetId()+
					"信息编号："+comment.getMsgId()+
					"评论内容："+comment.getCommentContent()+
					"评论人："+comment.getCommentAuthor()+
					"身份证："+comment.getAuthorIdcard()+
					"地址："+comment.getAuthorAddr()+
					"电子邮箱："+comment.getAuthorEmail()+
					"手机号码："+comment.getAuthorPhone()+",失败!", e);
			throw new BaseException(e,"添加评论失败");
		}
		return 0;
	}
}
